package com.example.filter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.constant.TokenConstant;
import com.example.secure.domain.SecureUser;
import com.example.utils.EncryptUtil;
import com.example.utils.core.ServletUtil;
import com.example.web.domain.Result;
import com.example.web.domain.ResultCode;
import com.example.web.exception.auth.token.TokenExpiredException;
import com.example.web.exception.auth.token.TokenValidationException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.annotation.Resource;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class TokenAuthenticationFilter extends OncePerRequestFilter {

    @Override
    protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse
            httpServletResponse, FilterChain filterChain) throws ServletException, IOException {
        String token = httpServletRequest.getHeader("json-token");
        // token param verify empty
        if (token == null) { ServletUtil.writeJson(Result.failure(ResultCode.TOKEN_MISSION)); return;}
        // 解析token
        String json = EncryptUtil.decodeUTF8StringBase64(token);
        JSONObject userJson = JSON.parseObject(json);
        // token verify
        SecureUser secureUser=new SecureUser();
        secureUser.setId(userJson.getString("userId"));

        JSONArray authoritiesArray = userJson.getJSONArray("authorities");
        String [] authorities = authoritiesArray.toArray( new
                String[authoritiesArray.size()]);

        //2.新建并填充authentication
        UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(
                secureUser, null, AuthorityUtils.createAuthorityList(authorities));

        authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(
                httpServletRequest));
        //3.将authentication保存进安全上下文
        SecurityContextHolder.getContext().setAuthentication(authentication);
        filterChain.doFilter(httpServletRequest, httpServletResponse);
    }
}
